package xyz.liut.liutlibraries.baseclass;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.ProgressBar;

import com.google.gson.Gson;

import xyz.liut.liutlibraries.ToastUtil;
import xyz.liut.liutlibraries.baseclass.globalcatcher.AppApi;
import xyz.liut.volley.LiutVolley;

/**
 * Created by liut on 2016/5/31.
 * baseActivity
 */

public class BaseActivity extends AppCompatActivity {

    private PopupWindow popupWindow;

    protected LiutVolley volley;
    public Gson gson;
    protected AppApi appApi;
    protected Context mContext;
    protected String TAG;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TAG = testGetFunctionName();
        volley = LiutVolley.getInstance(this);
        gson = new Gson();
        appApi = new AppApi(volley);
        mContext = this;
        BaseApplication.getInstance().addActivity(this);

        // 设置App只能竖屏显示
//        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }


    protected void setSuatusBarTranslucent() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }


    @Override
    protected void onPause() {
        super.onPause();
        volley.getRequestQueue().cancelAll(this);
    }

    @Override
    protected void onDestroy() {
        BaseApplication.getInstance().finishActivity(this);
        super.onDestroy();
    }

    protected void showProgress() {
        if (popupWindow == null) {
            popupWindow = new PopupWindow(mContext);
            popupWindow.setContentView(new ProgressBar(mContext));
            popupWindow.setFocusable(false);
            popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        }
        if (!popupWindow.isShowing()) {
            popupWindow.showAtLocation(new View(mContext), Gravity.CENTER, 0, 0);
        }
    }

    protected void hideProgress() {
        if (popupWindow != null) {
            popupWindow.dismiss();
        }
    }


    public void showToast(String content) {
        ToastUtil.showShortToast(this, content);
    }

    protected void showToast(int content) {
        ToastUtil.showShortToast(this, Integer.toString(content));
    }

    protected void showSnackBar(View view, String content) {
        Snackbar.make(view, content, Snackbar.LENGTH_SHORT).show();
    }

    protected void showSnackBar(View view, int content) {
        Snackbar.make(view, content + "", Snackbar.LENGTH_SHORT).show();
    }


    public String testGetFunctionName() {
        // 方法1：通过Throwable的方法getStackTrace()
        return new Throwable().getStackTrace()[1].getClassName();
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    assert v != null;
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        return getWindow().superDispatchTouchEvent(ev) || onTouchEvent(ev);
    }

    public boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = {0, 0};
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            return !(event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom);
        }
        return false;
    }

}
